package ms.jasim.console;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

import com.javadocking.DockingManager;
import com.javadocking.component.DefaultSwComponentFactory;
import com.javadocking.dock.Position;
import com.javadocking.dock.SplitDock;
import com.javadocking.dock.TabDock;
import com.javadocking.dockable.Dockable;
import com.javadocking.dockable.action.DefaultPopupMenuFactory;
import com.javadocking.model.FloatDockModel;
import com.javadocking.visualizer.FloatExternalizer;
import com.javadocking.visualizer.LineMinimizer;
import com.javadocking.visualizer.SingleMaximizer;

public class SanaDockPanel extends JPanel {
	
	private static final long serialVersionUID = 1L;
	private TabDock centerDock;
	

	/**
	 * Create the panel
	 */
	public SanaDockPanel(JFrame frame) {
		super(new BorderLayout());

		// Create the dock model for the docks.
		FloatDockModel dockModel = new FloatDockModel();
		dockModel.addOwner("frame0", frame);

		// Give the dock model to the docking manager.
		DockingManager.setDockModel(dockModel);
		centerDock = new TabDock();

		SplitDock splitDock = new SplitDock();
		
		// Add the root dock to the dock model.
		dockModel.addRootDock("splitdock", splitDock, frame);

		splitDock.addChildDock(centerDock, new Position(Position.RIGHT));
		
		// Add the dock to the panel.
		// add(splitDock, BorderLayout.CENTER);
		
		// Create an externalizer.
		FloatExternalizer externalizer = new FloatExternalizer(frame);
		dockModel.addVisualizer("externalizer", externalizer, frame);

		// Create a minimizer.
		LineMinimizer minimizer = new LineMinimizer(splitDock);
		dockModel.addVisualizer("minimizer", minimizer, frame);

		// Create a maximizer.
		SingleMaximizer maximizer = new SingleMaximizer(minimizer);
		dockModel.addVisualizer("maximizer", maximizer, frame);

		DefaultPopupMenuFactory popupMenuFactory = new DefaultPopupMenuFactory();
		popupMenuFactory.setPopupActions(DefaultPopupMenuFactory.DOCKABLE_ACTIONS | DefaultPopupMenuFactory.CLOSE_ALL_ACTION
				| DefaultPopupMenuFactory.CLOSE_OTHERS_ACTION);
		DefaultSwComponentFactory componentFactory = new DefaultSwComponentFactory();
		componentFactory.setPopupMenuFactory(popupMenuFactory);
		DockingManager.setComponentFactory(componentFactory);
		
		// Add the maximizer to the panel.
		this.add(maximizer, BorderLayout.CENTER);
		
	}
	
	public void addDockable(Dockable view) {
		if (!dockableExists(view))
			centerDock.addDockable(view, new Position(centerDock.getDockableCount()));
	}

	public boolean dockableExists(Dockable view) {
		for (int i = 0; i < centerDock.getDockableCount(); i++)
			if (centerDock.getDockable(i) == view)
				return true;
		return false;
	}

}
